package com.eric.service.impl;

import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.bean.BeanUtil;
import com.eric.domain.po.User;
import com.eric.domain.vo.UserVO;
import com.eric.mapper.UserMapper;
import com.eric.service.UserService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.eric.domain.po.table.UserTableDef.USER;

/**
 *  服务层实现。
 *
 * @author eric.deng
 * @since 2025-03-05
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>  implements UserService{

    @Override
    public SaResult getAllUserListByPageService(Integer cur, Integer pageSize) {
        Page<User> page = queryChain().select()
//                降序排列
                .from(User.class).orderBy(USER.UPDATE_TIME.desc()).page(new Page<>(cur, pageSize));
        List<User> records = page.getRecords();
        List<UserVO> userVOList = BeanUtil.copyToList(records, UserVO.class);
        for (UserVO userVO : userVOList) {
            userVO.setPassword(null);
        }
        return SaResult.data(userVOList);
    }

    @Override
    public SaResult getAllUserCountsService() {
        long count = queryChain().count();
        return SaResult.data(count);
    }

    @Override
    public SaResult getOneById(Long id) {
        User userInfo= queryChain().select().where(USER.ID.eq(id)).one();
        boolean empty = BeanUtil.isEmpty(userInfo);
        if (empty){
            return SaResult.error("不存在此账号！");
        }else {
            userInfo.setPassword(null);
            return SaResult.data(userInfo);
        }
    }

    @Override
    public SaResult getListByNameService(String name,Integer cur,Integer pageSize) {

        List<User> listByLoginName = queryChain().select()
                .where(USER.LOGIN_NAME.like(name,true))
                .orderBy(USER.UPDATE_TIME.desc())
                .page(new Page<>(cur, pageSize)).getRecords();
        List<User> listByRealName = queryChain().select()
                .where(USER.REAL_NAME.like(name,true))
                .orderBy(USER.UPDATE_TIME.desc())
                .page(new Page<>(cur, pageSize)).getRecords();

//       如果listByLoginName是空，直接返回listByRealName
        if(listByLoginName.isEmpty()){
            return SaResult.data(listByRealName);
        }
        else {
//            用set，主要是避免重复的元素，创建一个hashSet，同时把listByLoginName转换成Set类型并存进Set里面
            Set<User> userSet = new HashSet<>(listByLoginName);
            //  把ListByRealName存到Set中
            userSet.addAll(listByRealName);
            for (User user : userSet) {
//                把所有元素的密码设置为null
                user.setPassword(null);
            }
//           把set转换为List
            List<User> finalUserList=new ArrayList<>(userSet);

            return SaResult.data(finalUserList);
        }

    }

    @Override
    public SaResult getCountByName(String name) {
        List<User> listByLoginName = queryChain().select().where(USER.LOGIN_NAME.like(name,true)).list();
        List<User> listByRealName = queryChain().select().where(USER.REAL_NAME.like(name,true)).list();

//       如果listByLoginName是空，直接返回listByRealName
        if(listByLoginName.isEmpty()){
            return SaResult.data(listByRealName);
        }
        else {
//            用set，主要是避免重复的元素，创建一个hashSet，同时把listByLoginName转换成Set类型并存进Set里面
            Set<User> userSet = new HashSet<>(listByLoginName);
            //  把ListByRealName存到Set中
            userSet.addAll(listByRealName);
            for (User user : userSet) {
//                把所有元素的密码设置为null
                user.setPassword(null);
            }
//           把set转换为List
            List<User> finalUserList=new ArrayList<>(userSet);

            return SaResult.data(finalUserList.size());
    }
}

    @Override
    public SaResult getUserByBetweenTimesService(String startTime, String endTime,Integer cur,Integer pageSize) {

        List<User> list = queryChain().select()
                .from(User.class)
                .between("create_time",startTime,endTime)
                .orderBy(USER.UPDATE_TIME.desc())
                .page(new Page<>(cur, pageSize))
                .getRecords();
        return SaResult.data(list);
    }

    @Override
    public SaResult getCountByBetweenTimeService(String startTime, String endTime) {
        long count = queryChain().select().from(User.class).between("create_time", startTime, endTime)
                .count();
        return SaResult.data(count);
    }


}
